<template>
  <el-dialog
    @close="cancel"
    :title="title"
    :visible.sync="open"
    width="800px"
    class="dialogClass"
    append-to-body
  >
    <template slot="title">
      <div class="modalTitle">
        <img
          src="@/assets/bluelogo/drawerTitle.png"
          alt=""
          class="titleLogo"
        />{{ title }}
      </div>
    </template>
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="地区" prop="xzqhId" v-if="!xzqhId">
            <el-select v-model="formData.xzqhId" placeholder="请选择所属区域" clearable>
              <el-option v-for="qy in xzqhOptions" :key="qy.bm" :label="qy.qc" :value="qy.bm" />
            </el-select>
          </el-form-item>
          <el-form-item label="年份" prop="locationYear">
            <el-date-picker
              clearable
              v-model="formData.locationYear"
              type="year"
              value-format="yyyy"
              placeholder="请选择年份"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="立案数" prop="filcase">
            <el-input type="number" v-model="formData.filcase" placeholder="请输入立案数" oninput="value=value.replace(/[^0-9]/g,'')" />
          </el-form-item>
        </el-form>
    <div slot="footer">
      <div class="dialog-footer">
        <el-button @click="cancel" class="resetButton">取消</el-button>
        <el-button class="searchButton" @click="submitForm">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>

import {
  listLocation,
  getLocation,
  delLocation,
  addLocation,
  updateLocation,
  getDistrict,
} from "@/api/caseManage/caseLocation/location";

import {
  MODALTYPE,
  CASESTATE,
  PAGESIZE,
  PAGENUM,
  DEPTTYPE,
  ROLETYPE,
  CASEEXPORTURL,
} from "@/utils/variable";

import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import { getChildXzqh } from "@/api/system/dept";

export default {
  inheritAttrs: false,
  dicts: ["case_stage", "comple_status"],
  props: {
    modalType: {
      type: String,
      require: true,
    },
    id: {
      type: Number,
    },
    xzqhId: {
      type: String,
    }
  },

  data() {
    return {
      isDisabled: false,
      // 弹出层标题
      title: "",
      temCaseName: "",
      // 是否显示弹出层
      open: false,
      // 地区树选项
      xzqhOptions: [],
      formData: {
        id: "",
        xzqhId: "",
        locationYear: "",
        filcase: "",
        qc: "",
      },
      form: {},
      rules: {
        xzqhId: [{ required: true, message: "地区不能为空", trigger: "blur" }],
        locationYear: [
          { required: true, message: "年份不能为空", trigger: "blur" },
        ],
        filcase: [
          { required: true, message: "立案数不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    getChildXzqh("321").then((response) => {
      this.xzqhOptions = response.data;
    });
  },
  computed: {
    resetButtonContent() {
      if (this.modalType == MODALTYPE["详情"]) {
        return "关闭";
      } else {
        return "取消";
      }
    },
  },

  watch: {
    modalType: {
      handler(value) {
        if (value == MODALTYPE["新增"]) {
          this.reset();
          this.open = true;
          if(this.xzqhId !=null){
            this.formData.xzqhId = this.xzqhId
          }
          
          this.title = "新增立案数据";
        } else if (value == MODALTYPE["修改"]) {
          this.reset();
          if (this.id) {
            getLocation(this.id).then((response) => {
              this.formData = response.data;
              this.open = true;
              this.title = "修改立案数据";
            });
          }
        } else if (value == "detail") {
          this.isDisabled = true;
          this.reset();
          if (this.id) {
            getLocation(this.id).then((response) => {
              this.formData = response.data;
              this.open = true;
              this.title = "案件趋势详情";
            });
          }
        }
      },
      immediate: true,
    },
  },

  methods: {
    // 取消按钮
    cancel() {
      this.$emit("update:modalType", "");
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formData = {
        trendYear: "",
        filCase: "",
        caseAmount: "",
        casePeoples: "",
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.modalType == MODALTYPE["修改"]) {
            updateLocation(this.formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("update:modalType", "");
              this.$emit("fatherMethod");
            });
          } else if (this.modalType == MODALTYPE["新增"]) {
            addLocation(this.formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("update:modalType", "");
              this.$emit("fatherMethod");
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/dialog.scss"; //模态框样式

::v-deep.treeselect {
  font-size: 12px !important;
  font-family: $font-family !important;
  font-weight: $form-label-font-weight !important;
}
</style>
